package cn.tedu.restaurant.service.impl;

import cn.hutool.core.lang.UUID;
import cn.tedu.restaurant.entity.User;
import cn.tedu.restaurant.mapper.UserMapper;
import cn.tedu.restaurant.service.UserService;
import cn.tedu.restaurant.service.exception.*;
import cn.tedu.restaurant.utils.Md5Utils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author JachinLin
 * @since 2020-10-05
 */
@Service public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {
    @Autowired @Resource
    private UserMapper userMapper;

    @Override public void register(User user) throws InsertException, PhoneDuplicationException {

        User user1 = userMapper.selectByPhone(user.getPhone());
        if (user1 != null) {
            throw new PhoneDuplicationException("用户" + user.getPhone() + "已经被占用");
        } else {
            // user.getSalt();
            user.setIsDelete(0);
            user.setUsername(user.getUsername());
            user.setModifiedUser(user.getUsername());
            String salt = UUID.randomUUID().toString();
            user.setSalt(salt);
            user.setIsDelete(1);
            String md5Password = getMD5Password(user.getPassword(), salt);
            user.setPassword(md5Password);
            Integer rows = userMapper.insertUser(user);
            if (rows != 1) {
                throw new InsertException("插入用户异常");
            }
        }

    }

    @Override public User getByPhone(String phone) throws UserNotFoundException {
        return userMapper.selectByPhone(phone);
    }

    @Override public User getByUid(Integer uid) throws UserNotFoundException {
        return userMapper.findByUid(uid);
    }

    @Override public void changePassword(Integer uid, String oldPassword, String newPassword, String username)
        throws UserNotFoundException, PasswordNotMatchException, UpdateException {

        // TODO Auto-generated method stub
        User user = getByUid(uid);
        if (user == null) {
            throw new UserNotFoundException("用户不存在！");
        } else if (user.getIsDelete() != 1) {
            throw new UserNotFoundException("用户已经被冻结！");
        } else if (!user.getPassword().equals(getMD5Password(oldPassword, user.getSalt()))) {
            throw new PasswordNotMatchException("密码不匹配！");
        } else {
            String md5pwd = getMD5Password(newPassword, user.getSalt());
            // user.setPassword(md5pwd);
            Integer flag = userMapper.updatePassword(uid, md5pwd, username, new Date());
            if (flag != 1) {
                throw new UpdateException("修改失败！");
            }
        }
    }

    @Override public User login(String phone, String password) throws UserNotFoundException, PasswordNotMatchException {

        User user1 = getByPhone(phone);
        String salt = user1.getSalt();
        System.out.println(user1);
        if (getByPhone(phone) == null) {
            throw new UserNotFoundException("用户不存在！");
        } else if (user1.getIsDelete() != 1) {
            throw new UserNotFoundException("用户已经被冻结！");
        } else if (!getMD5Password(password, salt).equals(user1.getPassword())) {
            user1.setPassword(null);
            user1.setIsDelete(null);
            user1.setSalt(null);
            throw new PasswordNotMatchException("密码错误");
        }
        user1.setPassword(null);
        user1.setIsDelete(null);
        user1.setSalt(null);

        return user1;
    }

    @Override public void changeUserName(Integer uid,String username, String phone)
        throws UserNotFoundException, PasswordNotMatchException, UpdateException {
        if (userMapper.findByUid(uid)!=null){
        userMapper.updateUserName(uid,username,username,new Date());
        }
    }

    private String getMD5Password(String password, String salt) {
        String result = salt + password + salt;
        for (int i = 0; i < 5; i++) {
            result = Md5Utils.hash(result);
        }
        return result;
    }

}
